package com.sm.service.Impl;

import com.sm.dao.FinancialDao;
import com.sm.entity.Financial;
import com.sm.entity.FinancialRec;
import com.sm.service.FinancialService;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class FinancialServiceImpl implements FinancialService {

    @Autowired
    FinancialDao financialDao;

    public List<FinancialRec> findFinancialRec(String dateStr)throws Exception {
        return financialDao.findFinancialRec(dateStr);
    }

    public List<FinancialRec> findSevenDaysFRProfit()throws Exception {
        return financialDao.findSevenDaysFRProfit();
    }

    public List<FinancialRec> findSevenDaysFRIncome() throws Exception{
        return financialDao.findSevenDaysFRIncome();
    }

    public List<Financial> findFinancial(Integer month) throws Exception {
        return financialDao.findAllFinancial(month);
    }

    public Integer addFinancial(Financial financial) throws Exception {
        FinancialRec financialRec = new FinancialRec();
        FinancialRec profit = new FinancialRec();
        Double amount = financialDao.findTodayProfit();
        profit.setDate(new Date());
        profit.setSign(1);
        financialRec.setDate(new Date());
        financialRec.setAmount(financial.getAmount());
        Integer ie = financial.getIe();
        if(ie == 1){//未收
            financialRec.setSign(2);
            financialDao.updateFinancialRec(financialRec);
        }else if(ie ==2){//已收
            financialRec.setSign(4);
            profit.setAmount(amount+financial.getAmount());
            financialDao.updateFinancialRec(profit);
            financialDao.updateFinancialRec(financialRec);
        }else if(ie ==3){//未付
            financialRec.setSign(3);
            financialDao.updateFinancialRec(financialRec);
        }else if(ie ==4){//已付
            financialRec.setSign(5);
            profit.setAmount(amount-financial.getAmount());
            financialDao.updateFinancialRec(profit);
            financialDao.updateFinancialRec(financialRec);
        }
        return financialDao.addFinancial(financial);
    }

    public List<FinancialRec> financialStatistics() throws Exception {
        List<FinancialRec> list = financialDao.financialStatistics();
        return list;
    }

    public Double todayProfit(String dateStr) throws Exception {
        return financialDao.findTodayProfit();
    }

    public List<Financial> findNowFinancial(Integer limit,Integer month) throws Exception {
        return financialDao.findNowFinancial(limit,month);
    }

    public Integer findFinancialCount() throws Exception {
        return financialDao.findFinancialCount();
    }
}
